package solutions.jianzhi

import solutions.lib.TreeNode
import java.util.LinkedList

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/er-cha-shu-de-shen-du-lcof/">剑指 Offer 55 - I. 二叉树的深度</a>
 */
private class Solution55s1 {
    fun maxDepth(root: TreeNode?): Int {
        if (root == null) {
            return 0
        }
        val queue = LinkedList<TreeNode>().apply { offer(root) }
        var dep = 0
        while (queue.isNotEmpty()) {
            var p = queue.size
            dep++
            while (p-- > 0) {
                val node = queue.poll()
                node.left?.apply { queue.offer(this) }
                node.right?.apply { queue.offer(this) }
            }
        }
        return dep
    }
}

fun main() {
    println(Solution55s1().maxDepth(null))
}